//
//  cityModel.m
//  SQL(OC)
//
//  Created by 姚英 on 16/6/22.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "cityModel.h"
#import "SQLiteManager.h"

@implementation cityModel

- (BOOL)insertIntoDB{
    
    NSString *insert = [NSString stringWithFormat:@"INSERT INTO t_cityList (city, province, key) VALUES (\"%@\", \"%@\", \"%@\")",_city,_province,_key];
    
    return [[SQLiteManager shareSQLiteManager] exec:insert];
    
}

-(void)setCity:(NSString *)city{
    if (city.length > 0) {
        _city = city;
        
        //将汉字转换成拼音首字母
        NSMutableString *tempStr = [NSMutableString string];
        
        for (int i = 0; i < city.length; i++) {
            NSRange range = (NSRange){i,1};
            
            NSString *subStr = [city substringWithRange:range];
            
            // 将中文字符串转成可变字符串
            NSMutableString *pinyinText = [[NSMutableString alloc] initWithString:subStr];
            
            // 先转换为带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformMandarinLatin, NO);
            
            // 再转换为不带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformStripDiacritics, NO);

            NSString *firstStr = [pinyinText substringToIndex:1];
            [tempStr appendString:firstStr];

        }

        _key = tempStr;
    }
}

@end
